package com.mineplatform.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.mineplatform.domain.Consultation;
import com.mineplatform.domain.M_Consultation;
import com.mineplatform.domain.UserReply;

@Repository
public class M_ConsultationDao extends M_BaseDao {

	public M_Consultation findByFromTo(String from, String to) {
		// TODO Auto-generated method stub

		DetachedCriteria crit = DetachedCriteria.forClass(M_Consultation.class);
		crit.add(Restrictions.eq("From", from));
		crit.add(Restrictions.eq("To", to));
		List<?> list = super.getHibernateTemplate().findByCriteria(crit);
		if (list.size() > 0) {
			return (M_Consultation) list.get(0);
		} else {
			return null;
		}
	}

	public void save(M_Consultation model) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().save(model);
		
	}

	public void update(M_Consultation model) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().update(model);
	}

	public List<Consultation> findListByUserId(String fromUserId) {
		// TODO Auto-generated method stub
	
		List<Consultation> list = (List<Consultation>)this.getCurrentSession()
				.createQuery("select c.Id as Id,c.From as From,c.To as To,c.Price as Price,c.LastUpdateTime as LastUpdateTime,t.UserNickName as ToName,t.UserAvatar as ToAvatar from  M_Consultation as c inner join M_User as f on c.From=f.Id inner join M_User as t on c.To=t.Id "
						+ " where f.Id=:a order by c.LastUpdateTime desc")
				.setString("a",fromUserId)
				.setResultTransformer(Transformers.aliasToBean(Consultation.class))
				.list();

		return list;
	}
}
